﻿using System;
using MySql.Data.MySqlClient;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using scripts.logic;

namespace scripts.db
{
	public static class DbManager
	{
		public static MySqlConnection mysql;

		public static bool Connect(string db, string ip, int port, string user, string password)
		{
			mysql = new MySqlConnection();
			mysql.ConnectionString = string.Format("Database={0};Data Source={1}; port={2};User Id={3}; Password={4}", db, ip, port, user, password);
			try
			{
				mysql.Open();
				Console.WriteLine("[MySql] Connect Successful");
				return true;
			}
			catch (Exception e)
			{
				Console.WriteLine("[MySql] Connect Fail " + e.Message);
				return false;
			}
		}

		//判定安全字符串,防sql注入
		private static bool IsSafeString(string str)
		{
			return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
		}

		//是否存在该用户
		public static bool IsAccountExist(string id)
		{
			if (!IsSafeString(id))
			{
				Console.WriteLine("[MySql] IsAccountExist id Not Safe  " + id);
				return false;
			}
			string s = string.Format("select * from account where id='{0}';", id);
			//查询
			try
			{
				MySqlCommand cmd = new MySqlCommand(s, mysql);
				MySqlDataReader dataReader = cmd.ExecuteReader();
				bool hasRows = dataReader.HasRows;
				//若存在 hasRows返回true
				dataReader.Close();
				return hasRows;
			}
			catch (Exception e)
			{
				Console.WriteLine("[MySql] IsAccountExist Fail " + e.Message);
				return false;
			}
		}

		public static bool Register(string id, string password)
		{
			if (!IsSafeString(id))
			{
				Console.WriteLine("[MySql] Register id Not Safe " + id);
				return false;
			}
			if (!IsSafeString(password))
			{
				Console.WriteLine("[MySql] Register password Not Safe" + password);
				return false;
			}
			if (IsAccountExist(id))
			{
				Console.WriteLine("[MySql] Register AccountExist" + id);
				return false;
			}
			//写入MySql User表
			string sql = string.Format("insert into account set id ='{0}' ,pw ='{1}';", id, password);
			try
			{
				MySqlCommand cmd = new MySqlCommand(sql, mysql);
				cmd.ExecuteNonQuery();
				return true;
			}
			catch (Exception e)
			{
				Console.WriteLine("[MySql] Register Fail " + e.Message);
				return false;
			}
		}

		static JavaScriptSerializer Js = new JavaScriptSerializer();

		public static bool CreatePlayer(string id)
		{
			if (!IsSafeString(id))
			{
				Console.WriteLine("[MySql] CreatePlayer id Not Safe " + id);
				return false;
			}
			PlayerData playerData = new PlayerData();
			string data = Js.Serialize(playerData);
			//写入
			string sql = string.Format("insert into player set id ='{0}' ,data ='{1}';", id, data);
			try
			{
				MySqlCommand cmd = new MySqlCommand(sql, mysql);
				cmd.ExecuteNonQuery();
				return true;
			}
			catch (Exception e)
			{
				Console.WriteLine("[MySql] CreatePlayer Fail " + e.Message);
				return false;
			}
		}

		public static bool CheckPassword(string id, string password)
		{
			if (!IsSafeString(id))
			{
				Console.WriteLine("[MySql] CheckPassword id Not Safe " + id);
				return false;
			}
			if (!IsSafeString(password))
			{
				Console.WriteLine("[MySql] CheckPassword password Not Safe" + password);
				return false;
			}
			//查询
			string sql = string.Format("select * from account where id='{0}' and pw='{1}';", id, password);
			try
			{
				MySqlCommand cmd = new MySqlCommand(sql, mysql);
				MySqlDataReader dataReader = cmd.ExecuteReader();
				bool hasRows = dataReader.HasRows;
				dataReader.Close();
				return hasRows;
			}
			catch (Exception e)
			{
				Console.WriteLine("[MySql] CheckPassword Fail " + e.Message);
				return false;
			}
		}

		//获取玩家数据
		public static PlayerData GetPlayerData(string id)
		{
			if (!DbManager.IsSafeString(id))
			{
				Console.WriteLine("[MySql] GetPlayerData id Not Safe " + id);
				return null;
			}
			//查询
			string sql = string.Format("select * from player where id ='{0}';", id);
			try
			{
				MySqlCommand cmd = new MySqlCommand(sql, mysql);
				MySqlDataReader dataReader = cmd.ExecuteReader();
				if (!dataReader.HasRows)
				{
					dataReader.Close();
					return null;
				}
				//读取
				dataReader.Read();
				string data = dataReader.GetString("data");
				//反序列化
				PlayerData playerData = Js.Deserialize<PlayerData>(data);
				dataReader.Close();
				return playerData;
			}
			catch (Exception e)
			{
				Console.WriteLine("[MySql] GetPlayerData Fail " + e.Message);
				return null;
			}
		}

		//保存角色数据
		public static bool UpdatePlayerData(string id, PlayerData playerData)
		{
			//TODO 有无必要Id安全检测
			if (!DbManager.IsSafeString(id))
			{
				Console.WriteLine("[MySql] UpdatePlayerData id Not Safe " + id);
				return false;
			}
			//写入
			string data = Js.Serialize(playerData);
			string sql = string.Format("update player set data='{0}' where id ='{1}';", data, id);
			try
			{
				MySqlCommand cmd = new MySqlCommand(sql, mysql);
				cmd.ExecuteNonQuery();
				return true;
			}
			catch (Exception e)
			{
				Console.WriteLine("[MySql] UpdatePlayerData Fail " + e.Message);
				return false;
			}
		}
	}
}
